package com.hwl.vod.controller;

import com.hwl.commonutils.exception.GuliException;
import com.hwl.commonutils.result.RespBean;
import com.hwl.commonutils.result.ResultCodeEnum;
import com.hwl.vod.service.VodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Api(tags = "阿里云视频点播")
@RestController
@RequestMapping("/admin/vodMedia")
public class VodController {

    @Resource
    private VodService vodService;

    @ApiOperation("视频上传")
    @PostMapping("upload")
    public RespBean uploadVideo(@ApiParam(value = "文件", required = true) MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();

            String videoId = vodService.uploadVideo(inputStream, originalFilename);
            return RespBean.ok().data(videoId);
        } catch (IOException e) {
            throw new GuliException(ResultCodeEnum.VIDEO_UPLOAD_TOMCAT_ERROR);
        }
    }

    @ApiOperation("删除视频")
    @DeleteMapping("remove/{videoId}")
    public RespBean deleteVideo(@PathVariable String videoId) {
        vodService.removeVideo(videoId);
        return RespBean.ok().message("视频删除成功！");
    }

    @ApiOperation("批量删除视频")
    @DeleteMapping("deleteBatch")
    public RespBean deleteBatchVideo(@RequestBody List<String> videoIdList) {
        vodService.removeListVideo(videoIdList);
        return RespBean.ok().message("批量删除视频成功！");
    }
}
